package com.zzyl.nursing.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.ElderQueryDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;

/**
 * 老人Service业务层处理
 * 
 * @author JQ
 * @date 2025-08-5
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService
{
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询老人列表
     * @param dto 老人
     * @return 老人
     */
    @Override
    public TableDataInfo<Elder> selectElderList(ElderQueryDto dto)
    {
        //0、分页对象
        IPage<Elder> page = new Page<Elder>(dto.getPageNum(),dto.getPageSize());

        //分页查询
        page = elderMapper.selectPage(page,
                Wrappers.<Elder>lambdaQuery()
                        //1、根据名字做模糊搜索
                        .like(ObjectUtil.isNotEmpty(dto.getName()),Elder::getName, dto.getName())
                        //2、根据身份证号做精确搜索
                        .eq(ObjectUtil.isNotEmpty(dto.getIdCardNo()),Elder::getIdCardNo, dto.getIdCardNo())
        );

        //封装TableDataInfo
        TableDataInfo<Elder> dataInfo = new TableDataInfo<Elder>();
        dataInfo.setTotal(page.getTotal());
        dataInfo.setRows(page.getRecords());
        dataInfo.setCode(HttpStatus.SUCCESS);
        return dataInfo;
    }
    /**
     * 新增老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder)
    {
        return save(elder) ? 1 : 0;
    }

    /**
     * 修改老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder)
    {
        return updateById(elder) ? 1 : 0;
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}
